package com.gsm.service.impl;

import com.gsm.entity.SysMenu;
import com.gsm.dao.SysMenuDao;
import com.gsm.service.SysMenuService;
import com.gsm.entity.Result;
import com.gsm.utils.IdUtils;
import com.gsm.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 系统菜单(SysMenu)表服务实现类
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private IdUtils idUtils;
    
    
    @Override
    public Result insertSysMenu(SysMenu sysMenu) {
        long menuId = idUtils.nextId();
        sysMenu.setMenuId(menuId);
        sysMenuDao.insertSysMenu(sysMenu);
        Result result = new Result(true,StatusCode.OK);
        return result;
    }
    
    /**
     * 根据 主键 menuId 删除
     * @param menuId 主键
     * @return
     */
    @Override
    public Result deleteSysMenuByMenuId(Long menuId) {
        sysMenuDao.deleteSysMenuByMenuId(menuId);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }
    
    /**
     * 修改
     * @param sysMenu 实例对象
     * @return
     */
    @Override
    public Result updateSysMenuByMenuId(SysMenu sysMenu) {
        sysMenuDao.updateSysMenuByMenuId(sysMenu);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }
    
    /**
     * 根据 id 查询单条数据
     * @param menuId 主键
     * @return
     */
    @Override
    public Result selectSysMenuByMenuId(Long menuId) {
        SysMenu sysMenu = sysMenuDao.selectSysMenuByMenuId(menuId);
        Result result = new Result(true, StatusCode.OK,"",sysMenu);
        return result;
    }

    /**
     * 根据条件查询列表
     * @param sysMenu 实例对象
     * @return
     */
    @Override
    public Result selectSysMenuList(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuDao.selectSysMenuList(sysMenu);
        Result result = new Result(true, StatusCode.OK,"",list);
        return result;
    }

    @Override
    public Result selectSysMenuListByUserId(Long userId) {
        List<SysMenu> list = sysMenuDao.selectSysMenuListByUserId(userId);
        List<SysMenu> menuList = new ArrayList<>();
        List<SysMenu> sonList = new ArrayList<>();
        Iterator<SysMenu> iterator = list.iterator();
        while (iterator.hasNext()) {
            SysMenu next = iterator.next();
            if (next.getMenuPid() == 0) {
                menuList.add(next);
                iterator.remove();
            } else {
                sonList.add(next);
            }
        }

        menuList.forEach(i -> {
            Iterator<SysMenu> iterator1 = sonList.iterator();
            List<SysMenu> temList = new ArrayList<>();
            while (iterator1.hasNext()) {
                SysMenu next = iterator1.next();
                if (next.getMenuPid() == i.getMenuId()) {
                    temList.add(next);
                    iterator1.remove();
                }
            }
            i.setSysMenuList(temList);
        });

        Result result = new Result(true, StatusCode.OK, "", menuList);
        return result;
    }
}